// pages/my/index.js
import { accountinfo, getGrade, getInfo, userinfo } from '../../utils/request'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    objectArray: {},//年级
    array: ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级', '七年级'],
    index: 0,
    info: {},
    type:0
  },

  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type?Number(options.type):0
    })
    this.getInfoAll()
  },
  async getInfoAll(){
    const data = await getInfo()
    if (data.code !== '1') return
    this.setData({
      info: data.data
    },()=>{
    this.getnianjiList();
    })
  },
  // 获取年级列表
  async getnianjiList() {
    const data = await getGrade();
    var array = [];
    data.data.forEach(function (val, key, arr) {
      array.push({ id: key, name: val.title })
    })
    this.setData({
      objectArray: { title: "请选择年级", array, value: this.data.info.class_name }
    })
  },
  updataNj(e) {
    this.data.info.class_name = e.detail;
  },
  school(e) {
    this.data.info.school_name = e.detail.value;
  },
  name(e) {
    this.data.info.real_name = e.detail.value;
  },
  phone(e) {
    this.data.info.phone = e.detail.value;
  },
  async saveData() {
    let data = await userinfo({
      name: this.data.info.real_name,
      school: this.data.info.school_name,
      grade: this.data.info.class_name,
      mobile: this.data.info.phone
    });
    if (data.code === '1') {
      app.func.Toast.success("保存成功")
      const arr=["/pages/clocking/index","/pages/tougao/index",'/pages/deepbook/index']
      const {type}=this.data
      setTimeout(() => {
        if(type){
          wx.reLaunch({
            url:`${arr[type-1]}`,
          })
        }else{
          wx.switchTab({
            url: '/pages/my/index'
          })
        }
      
      }, 1000)
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})